package com.sgcc.sx.pmos.mobileapp.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.sgcc.sx.pmos.arouter.app.AppArouterPath;
import com.sgcc.sx.pmos.common.bean.UploadDataBean;
import com.sgcc.sx.pmos.common.http.DownloadUtil;
import com.sgcc.sx.pmos.common.http.HttpInterConst;
import com.sgcc.sx.pmos.common.http.HttpRequestUtils;
import com.sgcc.sx.pmos.common.http.MvcNetCallback;
import com.sgcc.sx.pmos.common.http.UploadUtil;
import com.sgcc.sx.pmos.common.widget.MyEditText;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.mobileapp.bean.DoLoginReleaseBean;
import com.sgcc.sx.pmos.mobileapp.bean.FileInfo;
import com.sgcc.sx.pmos.mobileapp.bean.LinkmanInfoBean;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class AddOrEditContactPopup extends CenterPopupView {

    private Switch mSwitchFitstcontact;
    private LinearLayout mLlFile;
    private LinearLayout mLlFileTitle;
    private ImageView mIvFile;
    private TextView mTvFile;
    private TextView mTvFiledownload;
    private MyEditText mEtName;
    private MyEditText mEtPhone;
    private MyEditText mEtMachineno;
    private MyEditText mEtEmail;
    private CheckBox mCb1;
    private CheckBox mCb2;
    private CheckBox mCb3;
    private CheckBox mCb4;
    private MyEditText mEtNote;
    private RelativeLayout mRlCancle;
    private RelativeLayout mRlSave;
    private LinkmanInfoBean.DataBean mBean;
    private int isCommomContact;
    private UploadDataBean mUploadDataBean;

    public interface OnCallBackListener {
        void save();
    }

    private OnCallBackListener mOnCallBackListener;

    public AddOrEditContactPopup(@NonNull Context context, OnCallBackListener onCallBackListener, LinkmanInfoBean.DataBean bean) {
        super(context);
        mOnCallBackListener = onCallBackListener;
        mBean = bean;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.customcenter_contact_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        findViewByIds();
        setViewAction();
        if (mBean != null) {

        }
    }

    private void setViewAction() {
        mLlFile.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build(AppArouterPath.APP_CHOOSEFILE)
                        .navigation();
            }
        });
        mRlCancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        mRlSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                isCommomContact = mSwitchFitstcontact.isChecked() ? 1 : 0;
                if (isCommomContact == 1) {
                    if (mUploadDataBean == null) {
                        ToastUtils.showShort("请选择授权文件");
                        return;
                    }
                }
                String contactName = mEtName.getText().toString().trim();
                if (TextUtils.isEmpty(contactName)) {
                    ToastUtils.showShort("请输入联系人姓名");
                    return;
                }
                String telephone = mEtPhone.getText().toString().trim();
                if (TextUtils.isEmpty(telephone)) {
                    ToastUtils.showShort("请输入手机号");
                    return;
                }
                if (!RegexUtils.isMobileExact(telephone)) {
                    ToastUtils.showShort("请输入正确的手机号");
                    return;
                }
                String officePhone = mEtMachineno.getText().toString().trim();
                if (TextUtils.isEmpty(officePhone)) {
                    ToastUtils.showShort("请输入办公电话");
                    return;
                }
                String email = mEtEmail.getText().toString().trim();
                if (TextUtils.isEmpty(email)) {
                    ToastUtils.showShort("请输入邮箱");
                    return;
                }
                if (mCb1.isChecked() == false && mCb2.isChecked() == false && mCb3.isChecked() == false && mCb4.isChecked() == false) {
                    ToastUtils.showShort("请选择专业");
                    return;
                }

                StringBuffer sb = new StringBuffer();
                if (mCb1.isChecked()) {
                    sb.append("1,");
                }
                if (mCb2.isChecked()) {
                    sb.append("2,");
                }
                if (mCb3.isChecked()) {
                    sb.append("5,");
                }
                if (mCb4.isChecked()) {
                    sb.append("6,");
                }

                String remarks = mEtNote.getText().toString().trim();
                String guid = null;
                if (mBean != null) {
                    guid = mBean.getGuid();
                }

                String fileId = null;
                if (mUploadDataBean != null) {
                    fileId = mUploadDataBean.getData().get(0).getFileId();
                }

                String contactType = sb.substring(0, sb.toString().length() - 1);

                saveLinkmanInfo(contactName, contactType, email, isCommomContact, "123456", fileId, officePhone, remarks, telephone, guid);
            }
        });

        mSwitchFitstcontact.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                changelayout();
            }
        });

        mTvFiledownload.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                DownloadUtil.get().downloadPdf(ActivityUtils.getTopActivity(), HttpInterConst.DOWNCONFIGFILE, new DownloadUtil.OnDownloadListener() {
                    @Override
                    public void onDownloadSuccess(File file) {
                        ToastUtils.showShort("文件下载成功 目录：" + file.getAbsolutePath());
                    }

                    @Override
                    public void onDownloading(int progress) {

                    }

                    @Override
                    public void onDownloadFailed(Exception e) {

                    }
                });
            }
        });
    }

    private void findViewByIds() {
        mSwitchFitstcontact = (Switch) findViewById(R.id.switch_fitstcontact);
        mLlFileTitle = (LinearLayout) findViewById(R.id.ll_file_title);
        mLlFile = (LinearLayout) findViewById(R.id.ll_file);
//        mIvFile = (ImageView) findViewById(R.id.iv_file);
        mTvFile = (TextView) findViewById(R.id.tv_file);
        mTvFiledownload = (TextView) findViewById(R.id.tv_filedownload);
        mEtName = (MyEditText) findViewById(R.id.et_name);
        mEtPhone = (MyEditText) findViewById(R.id.et_phone);
        mEtMachineno = (MyEditText) findViewById(R.id.et_Machineno);
        mEtEmail = (MyEditText) findViewById(R.id.et_email);
        mCb1 = (CheckBox) findViewById(R.id.cb_1);
        mCb2 = (CheckBox) findViewById(R.id.cb_2);
        mCb3 = (CheckBox) findViewById(R.id.cb_3);
        mCb4 = (CheckBox) findViewById(R.id.cb_4);
        mEtNote = (MyEditText) findViewById(R.id.et_note);
        mRlCancle = (RelativeLayout) findViewById(R.id.rl_cancle);
        mRlSave = (RelativeLayout) findViewById(R.id.rl_save);

        changelayout();
    }

    private void changelayout() {
        if (mSwitchFitstcontact.isChecked()) {
            mLlFile.setVisibility(VISIBLE);
            mTvFiledownload.setVisibility(VISIBLE);
            mLlFileTitle.setVisibility(VISIBLE);
        } else {
            mLlFile.setVisibility(GONE);
            mTvFiledownload.setVisibility(GONE);
            mLlFileTitle.setVisibility(GONE);
        }
    }

    public void setText(FileInfo bean) {
        mTvFile.setText(bean.getName());
        UploadUtil.upload("123456", "604", bean.getName(), "", bean.getName(), new File(bean.getPath()), new MvcNetCallback<UploadDataBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
            }

            @Override
            public void onResponse(UploadDataBean response, int id) {
                super.onResponse(response, id);
                if (null != response && null != response.getData() && response.getData().size() > 0) {
                    mUploadDataBean = response;
                }
            }
        });
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getPopupWidth() {
        return (int) (XPopupUtils.getScreenWidth(getContext()));
    }

    public void saveLinkmanInfo(String contactName, String contactType, String email, int isCommomContact, String membersId, String fileId, String officePhone, String remarks, String telephone, String guid) {
        Map<String, Object> params = new HashMap<>();
        params.put("contactName", contactName);
        params.put("contactType", contactType);
        params.put("email", email);
        params.put("isCommomContact", isCommomContact);
        params.put("membersId", membersId);
        params.put("officePhone", officePhone);
        params.put("remarks", remarks);
        params.put("telephone", telephone);
        params.put("guid", guid);
        params.put("fileId", fileId);

        params.put("contactTypeList", contactType.split(","));

        HttpRequestUtils.getInstance().request(params, HttpInterConst.SAVELINKMANINFO, new MvcNetCallback<DoLoginReleaseBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);

            }

            @Override
            public void onResponse(DoLoginReleaseBean response, int id) {
                super.onResponse(response, id);

                if (response != null) {
                    if (response.getStatus() == 0 && "Success".equals(response.getMessage())) {
                        if (mOnCallBackListener != null) {
                            mOnCallBackListener.save();
                            dismiss();
                        }
                    }
                }
            }
        });
    }
}
